package trunk;

import java.awt.Point;

public abstract class LivingThing<T> implements Saveable<T>, TimeDependent {
	protected Level currentLevel;
	protected Point location;

	public void setCurrentLevel(Level l) {
		currentLevel = l;
	}

	public void setLocation(Point p) {
		location = p;
	}

	public Cell[] getAdjacentCells() {
		Cell[] c = new Cell[8];
		c[0] = currentLevel.getCell(location.x, location.y - 1);
		c[1] = currentLevel.getCell(location.x + 1, location.y - 1);
		c[2] = currentLevel.getCell(location.x + 1, location.y);
		c[3] = currentLevel.getCell(location.x + 1, location.y + 1);
		c[4] = currentLevel.getCell(location.x, location.y + 1);
		c[5] = currentLevel.getCell(location.x - 1, location.y + 1);
		c[6] = currentLevel.getCell(location.x - 1, location.y);
		c[7] = currentLevel.getCell(location.x - 1, location.y - 1);
		return c;
	}

	public void moveN() {
		if (currentLevel.getCell(location.x, location.y - 1).isStandable()) {
			currentLevel.getCell(location.x, location.y).setLivingThing(null);
			currentLevel.getCell(location.x, location.y - 1).setLivingThing(
					this);
		}
		location = currentLevel.findLivingThing(this);
	}

	public void moveNE() {
		if (currentLevel.getCell(location.x + 1, location.y - 1).isStandable()) {
			currentLevel.getCell(location.x, location.y).setLivingThing(null);
			currentLevel.getCell(location.x + 1, location.y - 1)
					.setLivingThing(this);
		}
		location = currentLevel.findLivingThing(this);
	}

	public void moveE() {
		if (currentLevel.getCell(location.x + 1, location.y).isStandable()) {
			currentLevel.getCell(location.x, location.y).setLivingThing(null);
			currentLevel.getCell(location.x + 1, location.y).setLivingThing(
					this);
		}
		location = currentLevel.findLivingThing(this);
	}

	public void moveSE() {
		if (currentLevel.getCell(location.x + 1, location.y + 1).isStandable()) {
			currentLevel.getCell(location.x, location.y).setLivingThing(null);
			currentLevel.getCell(location.x + 1, location.y + 1)
					.setLivingThing(this);
		}
		location = currentLevel.findLivingThing(this);
	}

	public void moveS() {
		if (currentLevel.getCell(location.x, location.y + 1).isStandable()) {
			currentLevel.getCell(location.x, location.y).setLivingThing(null);
			currentLevel.getCell(location.x, location.y + 1).setLivingThing(
					this);
		}
		location = currentLevel.findLivingThing(this);
	}

	public void moveSW() {
		if (currentLevel.getCell(location.x - 1, location.y + 1).isStandable()) {
			currentLevel.getCell(location.x, location.y).setLivingThing(null);
			currentLevel.getCell(location.x - 1, location.y + 1)
					.setLivingThing(this);
		}
		location = currentLevel.findLivingThing(this);
	}

	public void moveW() {
		if (currentLevel.getCell(location.x - 1, location.y).isStandable()) {
			currentLevel.getCell(location.x, location.y).setLivingThing(null);
			currentLevel.getCell(location.x - 1, location.y).setLivingThing(
					this);
		}
		location = currentLevel.findLivingThing(this);
	}

	public void moveNW() {
		if (currentLevel.getCell(location.x - 1, location.y - 1).isStandable()) {
			currentLevel.getCell(location.x, location.y).setLivingThing(null);
			currentLevel.getCell(location.x - 1, location.y - 1)
					.setLivingThing(this);
		}
		location = currentLevel.findLivingThing(this);
	}

}
